<template>
    <el-dialog
      :title="form.id ? '编辑延迟返校登记' : '新增延迟返校登记'"
      :visible.sync="openDlg"
      width="800px"
      append-to-body
      destroy-on-close
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="学生姓名" prop="studentName">
                <el-input v-model="form.studentName" placeholder="请输入学生姓名" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="注册号" prop="registerCode">
                <el-input v-model="form.registerCode" placeholder="请输入注册号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="年级名称" prop="gradeName">
                <el-input v-model="form.gradeName" placeholder="请输入年级名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="班级名称" prop="className">
                <el-input v-model="form.className" placeholder="请输入班级名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="原计划返校时间" prop="oldDate">
                <el-date-picker
                    v-model="form.oldDate"
                    type="datetime"
                    value-format="yyyy-MM-dd HH:mm:ss"
                    placeholder="选择原计划返校时间">
                </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="最新返校时间" prop="newDate">
                <el-date-picker
                    v-model="form.newDate"
                    type="datetime"
                    value-format="yyyy-MM-dd HH:mm:ss"
                    placeholder="选择最新返校时间">
                </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="延迟返校原因" prop="reason">
                <el-input 
                    v-model="form.reason" 
                    type="textarea" 
                    :rows="3"
                    placeholder="请输入延迟返校原因" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="特殊人群标记" prop="flag">
                <el-select style="width:100%" v-model="form.flag" placeholder="请选择特殊人群标记">
                    <el-option label="正常" value="0"></el-option>
                    <el-option label="已标记" value="1"></el-option>
                </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="openDlg = false">取 消</el-button>
      </div>
    </el-dialog>
</template>

<script>
import { addDelayReturn, updateDelayReturn, getDelayReturn } from "@/api/dailyroutine/delayReturn.js";
export default {
  name: "addOrEditDlg",
  data() {
    return {
      openDlg: false,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        studentName: [
          { required: true, message: "学生姓名不能为空", trigger: "blur" }
        ],
        registerCode: [
          { required: true, message: "注册号不能为空", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    open(row) {
      this.reset();
      this.openDlg = true;
      if (row && row.id) {
        const id = row.id;
        getDelayReturn(id).then(response => {
          this.form = response.data;
        });
      }
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        studentName: null,
        registerCode: null,
        gradeName: null,
        className: null,
        oldDate: null,
        newDate: null,
        reason: null,
        flag: "0"
      };
      this.resetForm("form");
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateDelayReturn(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.openDlg = false;
              this.$emit("onSearch");
            });
          } else {
            addDelayReturn(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.openDlg = false;
              this.$emit("onSearch");
            });
          }
        }
      });
    }
  }
};
</script>